@file:JvmMultifileClass
@file:JvmName("JsonUtil")

package com.sunky.rfidapp.util

import com.google.gson.Gson
import java.lang.reflect.ParameterizedType
import java.lang.reflect.Type

fun Any.toJson(): String {
    var jsonStr = ""
    try {
        jsonStr = Gson().toJson(this) //现在替换为Gson
    } catch (e: Exception) {
//        loge(e)
    }
    return jsonStr
}

infix fun <T> String.toJsonObject(parseClass: Class<T>): T? {
    var t: T? = null
    try {
        t = Gson().fromJson(this, parseClass) //现在替换为Gson
    } catch (e: Exception) {
//        loge(e)
    }
    return t
}

infix fun <T> String.toJsonArray(parseClass: Class<T>): MutableList<T>? {
    var t: MutableList<T>? = null
    try {
        val listType = ParameterizedTypeImpl(MutableList::class.java, arrayOf(parseClass)) //现在替换为Gson
        t = Gson().fromJson(this, listType)
    } catch (e: Exception) {
//        loge(e)
    }
    return t
}

fun <T,R> Any.toMap():Map<T,R>{
    return this.toJson().toJsonObject(Map::class.java) as Map<T,R>
}

class ParameterizedTypeImpl(private val raw: Class<*>,
                            private val args: Array<Type> = arrayOf())
    : ParameterizedType {
    override fun getActualTypeArguments(): Array<Type> {
        return args
    }

    override fun getRawType(): Type {
        return raw
    }

    override fun getOwnerType(): Type? {
        return null
    }
}

inline infix fun Boolean?.yes(block: () -> Unit): Boolean? {
    if (this == true) {
        block()
    }
    return this
}

inline infix fun Boolean?.no(block: () -> Unit): Boolean? {
    if (this != true) {
        block()
    }
    return this
}